package streamtest;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class Test {
    public static void main(String[] args) throws IOException{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String string;

        StringBuffer stringBuffer = new StringBuffer();

        String filename = "file.txt";

        String content="";

        File file = new File(filename);



        do {
            string = bufferedReader.readLine();




            System.out.println("输入:"+string);

            write_fun(file,string);

            System.out.println("文件读取成功:"+read_fun(file));
        }while (!string.isEmpty());



    }

    public static String read_fun(File file) throws IOException {
        StringBuilder result = new StringBuilder();

        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String string = "";
            do {
                string = bufferedReader.readLine();
                result.append(string);
            } while (Objects.equals(string, ""));
//            bufferedReader.close();
//            inputStreamReader.close();

        }

        return result.toString();

    }

    public static void write_fun(File file,String content) throws IOException {

        try (FileOutputStream fileOutputStream = new FileOutputStream(file,true)) {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,StandardCharsets.UTF_8);

            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write(content);
            bufferedWriter.close();
            outputStreamWriter.close();
        }




    }
}
